package no.uib.hplims.views.settings;

import java.util.ArrayList;
import java.util.List;

import org.vaadin.appfoundation.persistence.facade.FacadeFactory;

import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Table;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.themes.Runo;

import no.uib.hplims.models.peptideinformation.Freezer;
import no.uib.hplims.tools.Icons;

public class FreezerSettings extends VerticalLayout {

	private static final long serialVersionUID = 8071550797417998296L;
	
	private Table freezerTable;
	private BeanItemContainer<Freezer> freezerContainer;
	private Button newButton = new Button("Add");
	private Button editAndSaveButton = new Button("Edit");
	
	private final ArrayList<Object> visibleColumnIds = new ArrayList<Object>();
	private final ArrayList<String> visibleColumnLabels = new ArrayList<String>();

	public FreezerSettings() {
		setSpacing(true);
		setCaption("Freezers");
		setMargin(true, true, false, true);
		
		Label header = new Label("Freezer settings");
		header.setStyleName(Runo.LABEL_H2);
		
		addComponent(header);
		addComponent(getToolbar());
		addComponent(getFreezerTable());
	}
	
	private HorizontalLayout getToolbar() {
		HorizontalLayout hl = new HorizontalLayout();
		
		newButton.addListener(newButtonClickListener);
		newButton.setIcon(Icons.newItemIcon16);
		
		editAndSaveButton.addListener(saveButtonClickListener);
		editAndSaveButton.setIcon(Icons.editIcon16);
		
		hl.addComponent(newButton);
		hl.addComponent(editAndSaveButton);
		
		return hl;
	}

	private Table getFreezerTable() {
		freezerTable = new Table();
		freezerTable.setStyleName(Runo.TABLE_SMALL);
		freezerTable.setWidth("50%");
		freezerTable.setEditable(false);
		freezerTable.setImmediate(true);
		
		List<Freezer> freezers = FacadeFactory.getFacade().list(Freezer.class);
		
		freezerContainer = new BeanItemContainer<Freezer>(Freezer.class);
		freezerContainer.addAll(freezers);
		
		freezerTable.setContainerDataSource(freezerContainer);
		
		visibleColumnIds.add("freezerName");
		visibleColumnLabels.add("Freezer");
		
		freezerTable.setVisibleColumns(visibleColumnIds.toArray());
		freezerTable.setColumnHeaders(visibleColumnLabels.toArray(new String[0]));
		
		return freezerTable;
	}
	
	private ClickListener newButtonClickListener = new ClickListener() {
		private static final long serialVersionUID = -3423406018440088612L;

		public void buttonClick(ClickEvent event) {
			Freezer freezer = new Freezer("Give the freezer a name...");
			FacadeFactory.getFacade().store(freezer);
			
			freezerContainer.addBean(freezer);
		}
	};
	
	private ClickListener saveButtonClickListener = new ClickListener() {
		private static final long serialVersionUID = 2651924440980564233L;

		public void buttonClick(ClickEvent event) {
			if (editAndSaveButton.getCaption().equals("Save")) {
				FacadeFactory.getFacade().storeAll(freezerContainer.getItemIds());
				
				editAndSaveButton.setCaption("Edit");
				editAndSaveButton.setIcon(Icons.editIcon16);
				
				freezerTable.setEditable(false);
			} else {
				editAndSaveButton.setCaption("Save");
				editAndSaveButton.setIcon(Icons.saveIcon16);
				
				freezerTable.setEditable(true);
			}
		}
	};
}
